package com.xin.controller;
import com.xin.annoations.Permission;
import com.xin.constants.CommonResult;
import com.xin.entity.crm.EmployEntity;
import com.xin.service.EmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 员工
 */
@RestController
@RequestMapping("/employee")
@Permission(value = "员工信息",sn = "employee")
@Api(tags = "员工相关", description = "员工")
public class EmployeeController {

    @Resource
    private EmployeeService employeeService;
    /**
     * 添加员工信息
     * @param employEntity
     * @return
     */
    @PostMapping(value = "/add" )
    @Permission(value = "添加员工信息",sn = "employee:add")
    @ApiOperation("添加员工信息")
    public CommonResult<?> add(@RequestBody  @Valid EmployEntity employEntity){
        return this.employeeService.add(employEntity)>=1?CommonResult.success(null,"员工添加成功"):CommonResult.error(499,"网络异常");
    }

    /**
     * 查询所有员工信息
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/findList")
    @ApiOperation("查询所有员工信息带分页")
    @Permission(value = "查询所有员工信息带分页",sn = "employee:findList")
    public CommonResult<?> findList(Integer page,Integer size){
        return CommonResult.success(this.employeeService.findAll(page,size),"请求成功");
    }

    /**
     * 根据员工名称模糊查询
     * @param name
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/findByNameLike")
    @Permission(value = "根据员工模糊查询数据带分页",sn = "employee:findByNameLike")
    @ApiOperation("根据员工模糊查询数据带分页")
    public CommonResult<?> findByNameLike(String  name,Integer page,Integer size){
        return CommonResult.success(this.employeeService.findByNameLike(name,page,size),"请求成功");
    }

    /**
     * 修改员工数据
     * @param employEntity
     * @return
     */
    @PutMapping("/update")
    @ApiOperation("修改员工数据")
    @Permission(value = "修改员工数据",sn = "employee:update")
    public CommonResult<?> update(@RequestBody EmployEntity employEntity){
        return this.employeeService.update(employEntity)>0?CommonResult.success(null,"修改员工共信息成功"):CommonResult.error(499,"网络异常");
    }


    /**
     * 批量删除员工信息
     * @param ids
     * @return
     */
    @PostMapping("/deleteBatch")
    @ApiOperation("批量删除员工数据")
    @Permission(value = "批量删除员工数据",sn = "employee:deleteBatch")
    public CommonResult<?> deleteBatch(@RequestBody List<Long> ids){
        return this.employeeService.deleteBatch(ids)>0?CommonResult.success(null,"删除员工信息成功"):CommonResult.error(499,"网络异常");
    }

    /**
     * 员工登录
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("员工登录")
    public CommonResult<?> employeeLogin(@RequestBody EmployEntity employEntity){
        return CommonResult.success(this.employeeService.employeeLogin(employEntity),"登录成功");
    }


    /**
     * 根据员工id查找
     * @param employeeId
     * @return
     */
    @GetMapping("/findById")
    @ApiOperation("根据员工id查找")
    @Permission(value = "根据员工id查找",sn = "employee:findById")
    public CommonResult<?> findByEmployeeId(Long employeeId){
        return CommonResult.success(this.employeeService.findById(employeeId),"请求成功");
    }

    /**
     * 导出全部excel数据
     * @return
     */
    @GetMapping("/exportAllData")
    @ApiOperation("导出全部员工数据")
    @Permission(value = "导出全部员工数据",sn = "employee:exportAllData")
    public void exportAllData(HttpServletResponse response){
        this.employeeService.exportAllData(response);
    }


    @PostMapping("/exportData")
    @ApiOperation("导出部分员工数据")
    @Permission(value = "导出员工数据",sn = "employee:exportData")
    public void exportData(List<EmployEntity> list,HttpServletResponse response){
        this.employeeService.exportData(list,response);
    }

    /**
     * 导入数据
     * @param multipartFile
     * @return
     */
    @PostMapping("/importData")
    @ApiOperation("导入员工数据")
    @Permission(value = "导入员工数据",sn = "employee:importData")
    public CommonResult<?> importData(MultipartFile multipartFile,HttpServletResponse response){
        return this.employeeService.importData(multipartFile,response)>0?CommonResult.success(null,"导入成功"):CommonResult.error(499,"导入失败");
    }



}
